package com.ita.iosc.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ita.iosc.bean.Client;

/**
 * Servlet implementation class registerServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String tel = request.getParameter("tel");
		String password = request.getParameter("password");
		String confirmPassword = request.getParameter("confirmPassword");
		String cname = request.getParameter("cname");
		HttpSession session = request.getSession();
		if(tel.length() == 0 || cname.length() == 0 || password.length() == 0 || password.length() == 0 || !password.equals(confirmPassword)){
			if(tel.length() == 0){
				request.setAttribute("telError", "Invalid format of telephone!");
			}
			if(cname.length() == 0){
				request.setAttribute("cnameError", "Invalid format of user name!");
			}
			if(password.length() == 0 || password.length() == 0){
				request.setAttribute("passwordError", "Invalid format of password!");
			}
			if(!password.equals(confirmPassword)){
				request.setAttribute("confirmPasswordError", "Passwords don't match!");
			}
//			response.sendRedirect("./register");
			request.getRequestDispatcher("./register").forward(request, response);
		}
		else {
			Client client = new Client(cname, tel, confirmPassword);
			session.setAttribute("user", client);
			response.sendRedirect("./listMerchant");
		}
	}

}
